﻿using AutoMapper;
using BPMN.Application.Contracts.DTO.Request.Material;
using BPMN.Application.Contracts.DTO.Response.Material;
using BPMN.Application.Contracts.DTO.Response;
using BPMN.Application.IService;
using BPMN.Domain.Entity.Material;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BPMN.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MaterialController : ControllerBase
    {
        public readonly IMaterialService _materialService;
        public readonly IProductsService _productsService;
        public MaterialController(IMaterialService materialService, IProductsService productsService)
        {
            _materialService = materialService;
            _productsService = productsService;
        }
        #region 物料
        /// <summary>
        ///物料 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddMaterial(MaterialRequestDto entity)
        {
            var list = await _materialService.AddMaterial(entity);

            return Ok(list);
        }
        /// <summary>
        /// 物料删除（单个）
        /// </summary>
        /// <param name="MId"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeleleMaterial(int MId)
        {
            return Ok(await _materialService.DeleleMaterial(MId));
        }

        /// <summary>
        /// 物料显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetMaterial(int pageindex, int pagesize, string? mEncode, string? mName, string? pName)
        {
            return Ok(await _materialService.GetMaterial(pageindex,pagesize,mEncode,mName,pName));
        }
        /// <summary>
        /// 物料显示（反填）
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetMaterialById(int Id)
        {
            return Ok(await _materialService.GetMaterialById(Id));
        }
        /// <summary>
        /// 物料修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UpdateMaterial(MaterialRequestDto entity)
        {
            return Ok(await _materialService.UpdateMaterial(entity));
        }
        #endregion
        #region 产品
        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="entity">产品信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddProducts(ProductsRequestDto entity)
        {
            return Ok(await _productsService.AddProducts(entity));
        }
        /// <summary>
        /// 产品下拉框
        /// </summary>
        /// <param name="PId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> XLKProducts(int PId)
        {
            return Ok(await _productsService.XLKProducts(PId));
        }
        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="PId">产品id</param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeleleProducts(int PId)
        {
            return Ok(await _productsService.DeleleProducts(PId));
        }
        /// <summary>
        /// 获取产品列表
        /// </summary>
        /// <param name="PFId">父id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetProducts(int PFId, string? PName, bool PYesNo)
        {
            var list = await _productsService.GetProducts(PFId, PName, PYesNo);
            if (!string.IsNullOrEmpty(PName))
            {
                 list.Data.Where(o => o.PName.Contains(PName)).ToList();
            }
            if (PYesNo != null)
            {
                 list.Data.Where(o => o.PYesNoEnable == PYesNo).ToList();
            }
            return Ok(list);

        }
        /// <summary>
        /// 反填产品
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetProductsById(int Id)
        {
            return Ok(await _productsService.GetProductsById(Id));
        }
        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UpdateProducts(ProductsRequestDto entity)
        {
            return Ok(await _productsService.UpdateProducts(entity));
        }
        #endregion
    }
}
